﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class DichVuDAO
    {
        private static QUANLYTIEMVANGEntities dbContext = new QUANLYTIEMVANGEntities();

        static public List<DICHVU> GetAllDichVu()
        {
            var query = from d in dbContext.DICHVUs
                        select d;

            if (query.ToList().Count == 0)
                return null;

            return query.ToList();
        }
        static public DICHVU SelectDichVuByMaDichVu(int maDV)
        {
            DICHVU dv = dbContext.DICHVUs.Find(maDV);
            return dv;
        }
        public static void Insert(DICHVU dv)
        {
            dbContext.DICHVUs.Add(dv);
            dbContext.SaveChanges();
        }
        public static void Delete(DICHVU dv)
        {
            if (dbContext.DICHVUs.Find(dv.MaDichVu) == null)
                return;

            dbContext.DICHVUs.Remove(dv);
            dbContext.SaveChanges();
        }
        public static void Update(DICHVU dv)
        {
            DICHVU tmpDV = dbContext.DICHVUs.Find(dv.MaDichVu);
            if (tmpDV != null)
            {
                tmpDV.TenDichVu = dv.TenDichVu;
                tmpDV.DonGiaDichVu = dv.DonGiaDichVu;
            }
        }
        public static int GetLastMaDichVu()
        {
            var query = (from d in dbContext.DICHVUs
                        select d.MaDichVu).ToList();
            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
